package KK.WindowSkin
{
	import KK.Cache.CacheManager;
	import KK.Hunter.Window.IWindow;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	import mx.core.Singleton;

	public class WindowSkinFactory
	{
		static private var _instance:WindowSkinFactory;
		
		static private var mWindowList:Dictionary=new Dictionary;
		
		private var _SkinList:Dictionary;
		
		public function WindowSkinFactory(singleton:Singleton)
		{
			if(_instance){
				LogError("this singleton class pless using Instance get class .");
			}
			_instance=this;
			init();
		}
		
		private function init():void{
			_SkinList=new Dictionary(); 
		}
		
		
		/**
		 * 根据名字创建窗体 
		 * @param WindowName
		 * 
		 */
		public function Creator(WindowName:String):IWindow{
			var result:IWindow;
			
			
			return result;
		}
		
		
		
		/**
		 * 创建皮肤  
		 * @param WindowName
		 * @return 
		 * 
		 */
		public function getSkin(windowName:String):WindowSkin{
			var result:WindowSkin;
			if(windowName){
				result=_SkinList[windowName];
			}
			
			return result;
		}
		
		/**
		 * 设置窗口皮肤  
		 * @param body
		 * @param title
		 * @param background
		 * 
		 */
		public function AddWindowSkin(windowName:String,body:BitmapData,title:BitmapData=null,background:BitmapData=null):void{
			var skin:WindowSkin=new WindowSkin();
			skin.Body=body;
			skin.Title=title;
			skin.Background=background;
			
			_SkinList[windowName]=skin;
			
		}
		
		/**
		 * 添加皮肤  
		 * @param windowName
		 * @param skin
		 * 
		 */
		public function addSkin(windowName:String,skin:WindowSkin):void{
			_SkinList[windowName]=skin;
		}
		/**
		 * 删除皮肤  
		 * @param windowName
		 * 
		 */
		public function RemoveWindowSkin(windowName:String):void{
			if(windowName){
				
				delete _SkinList[windowName];
			}
		}
		
		
		/**
		 * 注册  
		 * @param window
		 * 
		 */
		static public function Register(window:Window):void{
			
			var windowSkin:WindowSkin=WindowSkinFactory.GetSkin("window1");
			if(windowSkin){
				window.Skin=windowSkin;
			}else{
				mWindowList[window]=window;
			}
		}
		
		/**
		 * 处理窗体皮肤  
		 * 
		 */
		static public function HandleWindowSKin(WindowList:XMLList):void{
			var i:int=0;
			var len:int=WindowList.length();
			
			
			var name:String="";
			var skin:WindowSkin;
			var bm:Bitmap;
			
			while(i<len){
				skin=new WindowSkin();
				name = (WindowList[i].@name);
				
				bm=CacheManager.GetCache(WindowList[i].body.@name);
				skin.Body=bm?bm.bitmapData:null;
				
				bm=CacheManager.GetCache(WindowList[i].title.@name);
				skin.Title=bm?bm.bitmapData:null;
				
				bm=CacheManager.GetCache(WindowList[i].background.@name);
				skin.Background=bm?bm.bitmapData:null;
				
				bm=CacheManager.GetCache(WindowList[i].close.@name);
				skin.Close=bm?bm.bitmapData:null;
				if(name.length>0&&skin.Body){
					
					Instance.addSkin(name,skin);
				}
				
				
				i++;
			}
			
			var window:Window;
			
			for each(window in mWindowList){
				window.Skin=GetSkin("window1");
				delete mWindowList[window];
			}
			
		}
		
		/**
		 * 获取皮肤  
		 * @param windowName
		 * 
		 */
		static public function GetSkin(windowName:String):WindowSkin{
			var result:WindowSkin;
			
			result=Instance.getSkin(windowName);
			return result;
		}
		
		/**
		 * 删除皮肤 
		 * @param windowName
		 * 
		 */
		static public function RemoveSkin(windowName:String):void{
			Instance.RemoveWindowSkin(windowName);
		}
		
		static public function get Instance():WindowSkinFactory{
			if(!_instance){
				new WindowSkinFactory(new Singleton);
			}
			return _instance;
		}
	}
}


class Singleton{
	
}